package game.graphics;

import game.structures.Point;

import org.newdawn.slick.Image;

public class Frame {
	private Image image;
	private Point origin;
	private int end;
	private int next;
	
	/**
	 * 
	 * 
	 * @param image		Image that the frame will be derived from.
	 * @param r1		Point representing the top-left corner of the source rectangle on the image.
	 * @param r2		Point representing the width and height of the source rectangle on the image.
	 * @param origin	Offset from r1; the center point of the frame.
	 * @param scale		Visual width and height of the sprite.
	 * @param alpha		Visual Opacity of the frame.
	 * @param rotation	Visual angle that the frame will be drawn at.
	 */
	public Frame(Image image, Point r1, Point r2, Point origin, int end, int next) {
		this.end = end;
		this.next = next;

		this.image = image.getSubImage(
							(int)Math.floor(r1.getX()), 
							(int)Math.floor(r1.getY()), 
							(int)Math.floor(r2.getX()), 
							(int)Math.floor(r2.getY()));
		this.image.setCenterOfRotation(
				origin.getX(), 
				origin.getY());
	}

	public void setAlpha(float a) {
		image.setAlpha(a);
	}
	
	public void setRotation(float r) {
		image.setRotation(r);
	}
	
	public int getFrameEnd() {
		return end;
	}
	
	public float getWidth() { return image.getWidth(); }
	public float getHeight() { return image.getHeight(); }	
	
	public int getNext() {
		return next;
	}
	
	public void draw(float x, float y) {
		image.draw(
				(float)Math.floor(x), 
				(float)Math.floor(y));
	}
	
	public void draw(float x, float y, float xSkew, float ySkew) {
		float width = image.getWidth();
		float height = image.getHeight();
		
		Image temp = image.getScaledCopy(
				(int)(width * xSkew), 
				(int)(height * ySkew));
		temp.setAlpha(image.getAlpha());
		temp.setRotation(image.getRotation());
		temp.setCenterOfRotation(	
				xSkew * image.getCenterOfRotationX(), 
				ySkew * image.getCenterOfRotationY());
		temp.draw(
				(float)Math.floor(x - xSkew * image.getCenterOfRotationX()), 
				(float)Math.floor(y - ySkew * image.getCenterOfRotationY()));
	}
	
}
